package com.tianbing.adlottery.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.jubao.ejb.business.ProductBusiness;
import com.jubao.ejb.business.StoreBusiness;
import com.jubao.ejb.dao.CategoryDao;
import com.jubao.ejb.dao.CouponDao;
import com.jubao.ejb.dao.StoreDao;
import com.jubao.ejb.entity.Category;
import com.jubao.ejb.entity.Coupon;
import com.jubao.ejb.entity.CouponItem;
import com.jubao.ejb.entity.ProductPrice;
import com.jubao.ejb.entity.Store;
import com.tianbing.adlottery.helper.SkinUtils;

/**
 * MultiActionController for the image list/upload UI.
 * 
 * @author Conan Chen
 * @since 2006.12.15
 */
public class StoreController extends MultiActionController {
	Logger logger = Logger.getLogger(StoreController.class.getName());

	private String storeDetailView;
	private String couponDetailView;
	private String couponItemDetailView;

	private StoreDao storeDao;
	private CouponDao couponDao;

	private CategoryDao categoryDao;

	private SkinUtils skinUtils;

	private ProductBusiness productBusiness;
	private StoreBusiness storeBusiness;

	@SuppressWarnings("unchecked")
	public ModelAndView viewStoreDetail(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(storeDetailView);
		Map model = mv.getModel();

		// --get request parameter
		Long stosid = skinUtils.getParameterStoreSysId(request);
		Long argsid = skinUtils.getParameterAreaCategorySysId(request);
		Long bsgsid = skinUtils.getParameterBusinessCategorySysId(request);
		Integer pgno = ServletRequestUtils.getIntParameter(request, "pgno", 0);
		int pageSize = 30;
		// --/get request parameter

		Store store = storeDao.find(stosid);
		if (store != null) {
			//1. get store info
			Category areaCategory = categoryDao.find(argsid);
			//get category primary trail
			List<Category> areaPrimaryCategoryTrail = null;
			if (areaCategory != null) {
				areaPrimaryCategoryTrail = categoryDao.getPrimaryCategoryTrail(argsid);
			}

			Category businessCategory = categoryDao.find(bsgsid);

			//2. get product info
			List<ProductPrice> productPriceList = productBusiness.getProductPriceList(-1, stosid, -1, pgno, pageSize);
			long totalCount = productBusiness.getProductPriceCount(-1, stosid, -1);

			//3. get coupon list
			List<Coupon> couponList = storeBusiness.getCouponList(stosid, pgno, 30);
			model.put("couponList", couponList);
			
			model.put("store", store);
			model.put("areaCategory", areaCategory);
			model.put("areaPrimaryCategoryTrail", areaPrimaryCategoryTrail);
			model.put("businessCategory", businessCategory);

			model.put("productPriceList", productPriceList);

			int pageItemNum = productPriceList == null ? 0 : productPriceList.size();
			String otherParam = "stosid=" + stosid + "&argsid=" + argsid + "&bsgsid=" + bsgsid;
			model = skinUtils.preparePageFooterParameters(request, pageItemNum, totalCount, pgno, pageSize, otherParam,
					model);

		}
		return mv;
	}

	public ModelAndView viewCouponDetail(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(couponDetailView);
		Map model = mv.getModel();

		// --get request parameter
		Long cpsid = ServletRequestUtils.getLongParameter(request, "cpsid",-1);
		Integer pgno = ServletRequestUtils.getIntParameter(request, "pgno", 0);
		int pageSize = 30;
		// --/get request parameter

		Coupon coupon = couponDao.findCoupon(cpsid);
		if (coupon != null) {

			model.put("coupon", coupon);
			List<CouponItem> couponItemList = storeBusiness.getCouponItemList(-1,cpsid,-1, pgno, pageSize);
			long totalCount = storeBusiness.getCouponItemCount(-1,cpsid,-1);
			model.put("couponItemList", couponItemList);

			int pageItemNum = couponItemList == null ? 0 : couponItemList.size();
			String otherParam = "cpsid="+cpsid;
			model = skinUtils
					.preparePageFooterParameters(request, pageItemNum, totalCount, pgno, pageSize, otherParam, model);
		}
		return mv;
	}

	public ModelAndView viewCouponItemDetail(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(couponItemDetailView);
		Map model = mv.getModel();

		// --get request parameter
		Long cpisid = ServletRequestUtils.getLongParameter(request, "cpisid",-1);

		CouponItem couponItem = couponDao.findCouponItem(cpisid);
		model.put("couponItem", couponItem);
		return mv;
	}

	public void setLogger(Logger logger) {
		this.logger = logger;
	}

	public void setStoreDetailView(String storeDetailView) {
		this.storeDetailView = storeDetailView;
	}

	public void setStoreDao(StoreDao storeDao) {
		this.storeDao = storeDao;
	}

	public void setCategoryDao(CategoryDao categoryDao) {
		this.categoryDao = categoryDao;
	}

	public void setSkinUtils(SkinUtils skinUtils) {
		this.skinUtils = skinUtils;
	}

	/**
	 * @param productBusiness the productBusiness to set
	 */
	public void setProductBusiness(ProductBusiness productBusiness) {
		this.productBusiness = productBusiness;
	}

	public void setStoreBusiness(StoreBusiness storeBusiness) {
		this.storeBusiness = storeBusiness;
	}

	public void setCouponDetailView(String couponDetailView) {
		this.couponDetailView = couponDetailView;
	}

	public void setCouponDao(CouponDao couponDao) {
		this.couponDao = couponDao;
	}

	public void setCouponItemDetailView(String couponItemDetailView) {
		this.couponItemDetailView = couponItemDetailView;
	}

}
